Retrieving Distinct Field Values

Description

A common application requirement is to produce a list of unique field values contained in a table or set of records. The SQL language uses the DISTINCT keyword to produce these lists.

  1. Open the SQL Genie and display its Tables tab.

    dim qry as C
    dim conn as SQL::Connection
    conn.open("{A5API=Access,FileName='C:\Program Files\a5v8\MDBFiles\Alphasports.mdb',UserName='Admin'}")
    qry = sql_query_builder(conn)
  2. Click Add Table to display the Add Table dialog.

  3. Select "Customer" and click OK. The SQL window now shows:

    SELECT *
    FROM Customer
  4. Display the Columns tab.

  5. Click the '<<' button to remove the asterisk "*" from the Selected Columns list.

  6. Select "BILL_STATE_REGION" in the Available Columns list and click the >' button. The SQL window now shows:

    SELECT [SHIP_STATE_REGION]
    
    FROM Customer
  7. Display the Order tab.

  8. Select "BILL_STATE_REGION" in the Available Columns list and click the '>' button. The SQL window now shows:

    SELECT [BILL_STATE_REGION]
    FROM Customer   
    ORDER BY [BILL_STATE_REGION]
  9. Display the Properties tab.

  10. Select the Distinct? check box. The SQL window now shows:

    SELECT DISTINCT [BILL_STATE_REGION]
    FROM Customer   
    ORDER BY [BILL_STATE_REGION]
  11. Click Execute Query.

  12. Click Close to exit the SQL Genie.

  13. Run the next statements to retrieve and display the data.

    conn.execute(qry)
    sql_resultset_preview(conn.ResultSet)
  14. You will see an alphabetized list of unique state values.

Limitations

Desktop applications only.

See Also